package com.bookm.bookm.controller;

import com.bookm.bookm.entity.Reader;
import com.bookm.bookm.entity.Result;
import com.bookm.bookm.service.ReaderService;
import com.bookm.bookm.service.impl.ReaderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 本 servlet 中包含了所有有关 用户信息的操作
 */
@WebServlet("/reader")
public class ReaderServlet extends HttpServlet {
    /**
     * 用户操作类
     */
    private ReaderService service = new ReaderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 这里不区分 get 和 post 方法 一并在doPost中实现
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        String key = req.getParameter("key");
        switch (key) {
            // 用户注册
            case "register":
                register(req, out);
                break;
            // 用户登录
            case "login":
                login(req, out);
                break;
            // 用户信息列表获取
            case "getReaderList":
                getReaderList(out);
                break;
            // 启用、禁用用户
            case "banUser":
                String id = req.getParameter("id");
                boolean is = Boolean.parseBoolean(req.getParameter("is"));
                out.print(service.banUser(id, is));
                break;
            // 更新用户信息
            case "updateUser":
                updateUser(req, out);
                break;
            // 根据用户学号获取用户信息
            case "getReaderById":
                getReaderById(req, out);
        }
    }

    // 根据用户学号获取用户信息
    private void getReaderById(HttpServletRequest req, PrintWriter out) {
        String id = req.getParameter("id");
        Result usr = service.getUsr(id);
        out.print(usr);
    }

    // 更新用户信息
    private void updateUser(HttpServletRequest req, PrintWriter out) {
        // 学号
        String id = req.getParameter("stdId");
        // 姓名
        String name = req.getParameter("name");
        // 专业id
        int professional = Integer.parseInt(req.getParameter("professional"));
        // 学院id
        int college = Integer.parseInt(req.getParameter("college"));
        // 邮箱
        String email = req.getParameter("email");
        // 手机号
        String phone = req.getParameter("phone");
        // 角色
        String roles = req.getParameter("roles");
        Reader reader = new Reader(id, name, professional
                , college, email, phone, null, roles);
        out.print(service.updateUser(reader));
    }
    // 用户信息列表获取
    private void getReaderList(PrintWriter out) {
        Result result = service.getUserList();
        out.print(result);
    }
    // 用户注册
    private void register(HttpServletRequest req, PrintWriter out) {
        // 姓名
        String username = req.getParameter("name");
        // 邮箱
        String email = req.getParameter("email");
        // 手机号
        String phone = req.getParameter("phone");
        // 学号
        String stdId = req.getParameter("stdId");
        // 密码
        String pwd = req.getParameter("pwd");
        // 学院id
        Integer college = Integer.parseInt(req.getParameter("college"));
        // 专业id
        Integer prof = Integer.parseInt(req.getParameter("professional"));
        Reader reader = new Reader(stdId, username, prof, college, email, phone, pwd, "user");
        Result register = service.register(reader);
        out.print(register.toString());
    }
    // 用户登录
    private void login(HttpServletRequest req, PrintWriter out) {
        String stdId = req.getParameter("stdId");
        String pwd = req.getParameter("pwd");
        Reader reader = new Reader();
        reader.setId(stdId);
        reader.setEmail(stdId);
        reader.setPhone(stdId);
        reader.setName(stdId);
        reader.setPwd(pwd);
        Result login = service.login(reader);
        out.print(login.toString());
    }
}
